﻿/*******************************************************************************
 * Creator:admin 2022-05-07 19:30:22
 * Description: YoursLC有源低代码
*********************************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Table;
using Think9.Services.Flow;

namespace Think9.Services.CodeBuild
{
	/// <summary>
	/// 主表Service
	/// </summary>
	public class Demo51Service : BaseService<Demo51Model>
	{
		private ComService ComService = new ComService();
		private readonly string split = BaseUtil.ComSplit;//字符分割 用于多选项的分割等 如split+选项1+split+选项2+split
		private string sql;

		#region Before处理
		/// <summary>
		/// 添加前数据处理，默认处理自动编号即子表数据初始化
		/// </summary>
		/// <param name="err">错误信息</param>
		/// <param name="flowid">流程编码</param>
		/// <param name="maintbid">主表编码</param>
		/// <param name="tbname">主表名称</param>
		/// <param name="mPrcs">当前流程步骤，基础信息表为空</param>
		/// <param name="CurrentUser">当前用户</param>
		/// <returns></returns>
		public int BeforeAdd(ref string err, string flowid, string maintbid, string tbname, CurrentPrcsEntity mPrcs, CurrentUserEntity CurrentUser)
		{
			int listid = 0;

			//处理自动编号-- 如果有则添加一条数据,返回自增长的listid;如果没有则返回0
			listid = AutoNo.SetAutoNumber(ref err, flowid, tbname, mPrcs, CurrentUser);



			return listid;
		}

		/// <summary>
		/// 编辑前数据处理，默认使用通用函数标志接手办理，可自定义
		/// </summary>
		/// <param name="flowid">流程编码</param>
		/// <param name="listid">主表数据id</param>
		/// <param name="mPrcs">当前流程步骤，基础信息表为空</param>
		/// <param name="CurrentUser">当前用户</param>
		/// <returns></returns>
        public string BeforeEdit(string flowid, string listid, CurrentPrcsEntity mPrcs, CurrentUserEntity CurrentUser)
        {
            string err = "";
            if (flowid.StartsWith("fw_"))
            {
                err = FlowCom.TakeOverPrcs(CurrentUser, mPrcs);//使用通用函数标志接手办理
            }
			//可自定义

            return err;
        }

		/// <summary>
		/// 删除前数据处理，默认什么也不做，可自定义
		/// </summary>
		/// <param name="flowid">流程编码</param>
		/// <param name="listid">主表数据id</param>
		/// <param name="mPrcs">当前流程步骤，基础信息表为空</param>
		/// <param name="CurrentUser">当前用户</param>
		/// <returns></returns>
        public string BeforeDelete(string flowid, string listid, CurrentPrcsEntity mPrcs, CurrentUserEntity CurrentUser)
        {
            string err = "";
			//可自定义
            return err;
        }
		#endregion Before处理

		#region After处理
		/// <summary>
		/// 编辑后数据处理，默认调用通用函数实现数据回写，可自定义
		/// </summary>
		/// <param name="flowid">流程编码</param>
		/// <param name="listid">主表数据id</param>
		/// <param name="prcsid">当前流程步骤id</param>
		/// <param name="CurrentUser">当前用户</param>
		/// <returns></returns>
		public string AfterEdit(string listid, string flowid, string prcsid, CurrentUserEntity user)
        {
			string err = "";

			//调用通用函数实现数据回写，用户可自定义 数据回写在录入表管理/数据读写中设置
			WriteBack WriteBack = new WriteBack();
			string maintbid = flowid.Replace("bi_", "tb_").Replace("fw_", "tb_");
			err = WriteBack.WriteValueByRelationSet(listid, flowid, maintbid, prcsid, user);

			//可自定义

			return err;
		}

		/// <summary>
		/// 完成后数据处理，默认调用通用函数实现数据回写，可自定义
		/// </summary>
		/// <param name="flowid">流程编码</param>
		/// <param name="listid">主表数据id</param>
		/// <param name="prcsid">当前流程步骤id</param>
		/// <param name="CurrentUser">当前用户</param>
		/// <returns></returns>
		public string AfterFinish(string listid, string flowid, string prcsid, CurrentUserEntity user)
		{
			string err = AfterEdit(listid, flowid, prcsid, user);
			if(string.IsNullOrEmpty(err))
            {
				//数据锁定等
				FlowCom.FinishFlowPrcs(listid, flowid, prcsid, user);

				//可自定义
            }

			return err;
		}

		/// <summary>
		/// 删除后数据处理，默认什么也不做，可自定义
		/// </summary>
		/// <param name="flowid">流程编码</param>
		/// <param name="listid">主表数据id</param>
		/// <param name="mPrcs">当前流程步骤，基础信息表为空</param>
		/// <param name="CurrentUser">当前用户</param>
		/// <returns></returns>
		public string AfterDelete(string flowid, string listid, CurrentPrcsEntity mPrcs, CurrentUserEntity CurrentUser)
		{
			string err = "";
			//可自定义
			return err;
		}
		#endregion After处理

		#region 查询
		/// <summary>
		/// 主表查询调用，返回列表，查询条件可在录入表指标属性中设置
		/// </summary>
		/// <param name="total">每页行数</param>
		/// <param name="model">主表数据model</param>
		/// <param name="pageInfo">页面信息，包括行数、排序等</param>
		/// <param name="user">用户id</param>
		/// <param name="flowid">流程编码</param>
		/// <param name="tbid">主表编码</param>
		/// <returns></returns>
		public IEnumerable<dynamic> GetPageListBySearch(ref long total, Demo51Model model, PageInfoEntity pageInfo, CurrentUserEntity user, string flowid, string tbid)
		{
			pageInfo.returnFields = "FlowRunList.isLock,FlowRunList.BeginTime, FlowRunList.BeginUser," + tbid + ".* ";
			string where = BasicHelp.GetWhereByFlowId(user, flowid);
			string some = "  FlowRunList INNER JOIN " + tbid + " ON FlowRunList.listid = " + tbid + ".listid ";

			//编号
			if (!string.IsNullOrEmpty(model.inNumber))
			{
				where += " and (tb_Demo51.inNumber like @inNumber)"; 
				model.inNumber = string.Format("%{0}%", model.inNumber); 
			}
			//申请人
			if (!string.IsNullOrEmpty(model.inApplicant))
			{
				where += " and (tb_Demo51.inApplicant like @inApplicant)"; 
				model.inApplicant = string.Format("%{0}%", model.inApplicant); 
			}
			//申请部门
			if (!string.IsNullOrEmpty(model.inApplicationDepartment))
			{
				where += " and (tb_Demo51.inApplicationDepartment like @inApplicationDepartment)"; 
				model.inApplicationDepartment = string.Format("%{0}%", model.inApplicationDepartment); 
			}
			//申请日期
			if(model.inApplicationDate != null)
			{
				where += " and (tb_Demo51.inApplicationDate >= @inApplicationDate)"; 
			}
			if(model.inApplicationDate_Exa  != null)
			{
				where += " and (tb_Demo51.inApplicationDate <= @inApplicationDate_Exa)"; 
			}
			//开始日期
			if(model.inStartDate != null)
			{
				where += " and (tb_Demo51.inStartDate >= @inStartDate)"; 
			}
			if(model.inStartDate_Exa  != null)
			{
				where += " and (tb_Demo51.inStartDate <= @inStartDate_Exa)"; 
			}
			//结束日期
			if(model.inEndDate != null)
			{
				where += " and (tb_Demo51.inEndDate >= @inEndDate)"; 
			}
			if(model.inEndDate_Exa  != null)
			{
				where += " and (tb_Demo51.inEndDate <= @inEndDate_Exa)"; 
			}
			//事由及目的
			if (!string.IsNullOrEmpty(model.inCauseAndPurpose))
			{
				where += " and (tb_Demo51.inCauseAndPurpose like @inCauseAndPurpose)"; 
				model.inCauseAndPurpose = string.Format("%{0}%", model.inCauseAndPurpose); 
			}
			//费用预计
			if (!string.IsNullOrEmpty(model.inCostEstimate))
			{
				where += " and (tb_Demo51.inCostEstimate like @inCostEstimate)"; 
				model.inCostEstimate = string.Format("%{0}%", model.inCostEstimate); 
			}
			//交通工具
			if (!string.IsNullOrEmpty(model.inVehicle))
			{
				where += " and (tb_Demo51.inVehicle like @inVehicle)"; 
				model.inVehicle = string.Format("%{0}%", model.inVehicle); 
			}
			//备注
			if (!string.IsNullOrEmpty(model.inRemarks))
			{
				where += " and (tb_Demo51.inRemarks like @inRemarks)"; 
				model.inRemarks = string.Format("%{0}%", model.inRemarks); 
			}
			//主管审批
			if (!string.IsNullOrEmpty(model.inSupervisorApproval))
			{
				where += " and (tb_Demo51.inSupervisorApproval like @inSupervisorApproval)"; 
				model.inSupervisorApproval = string.Format("%{0}%", model.inSupervisorApproval); 
			}
			//主管意见
			if (!string.IsNullOrEmpty(model.inSupervisorOpinion))
			{
				where += " and (tb_Demo51.inSupervisorOpinion like @inSupervisorOpinion)"; 
				model.inSupervisorOpinion = string.Format("%{0}%", model.inSupervisorOpinion); 
			}
			return base.GetPageUnite(ref total, model, pageInfo, some + where);

		}

		/// <summary>
		/// 主表-为下拉选择、多选、单选准备动态数据源
		/// 处理列表显示时value与text转化，备注：动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="from">edit或list</param>
		/// <param name="param">条件参数</param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();


			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 弹出页面的查询条件有下拉选择、多选、单选时，为其绑定动态数据源
		/// 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="tbid">_main或者空</param>
		/// <param name="indexid">指标编码</param>
		/// <param name="from">edit或list</param>
		/// <param name="param">条件参数</param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string tbid, string indexid, string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();

			//主表指标弹出页面
			if (tbid == "_main")
			{

			}
			else//子表指标弹出页面
			{

			}

			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}
		#endregion 查询

		/// <summary>
		/// 数据删除
		/// </summary>
		/// <param name="id">主表数据id</param>
		/// <param name="fwid">流程编码</param>
		/// <returns></returns>
		public bool DeleteByID(string id, string fwid)
		{
			AttachmentService.DelAttachment(int.Parse(id), fwid);//删除附件

			//删除flowrunlist、flowrunprcslist中关联数据
			if (fwid.StartsWith("fw_"))
			{
				ComService.ExecuteSql("delete from flowrunlist where ListId = " + id + "");
				ComService.ExecuteSql("delete from flowrunprcslist where ListId = " + id + "");
			}

			AutoNo.DelAutoNo(id, fwid);//删除自动编号

			return base.DeleteByWhere("where ListId=" + id + "");//删除主表数据
		}

		/// <summary>
		///返回可修改字段 自由流程排除了隐藏指标 固定流程排除了隐藏指标及不可写字段
		/// </summary>
		/// <param name="prcno">流程步骤编码</param>
		/// <returns></returns>
		public string GetUpdateFields(string prcno)
		{
			//固定流程排除了隐藏指标、隐藏及不可写字段 
			string UpdateFields = "";
			//申请 步骤编码new
			if (prcno == "new") 
			{
				UpdateFields = "inNumber,inApplicant,inApplicationDepartment,inApplicationDate,inStartDate,inEndDate,inCauseAndPurpose,inCostEstimate,inVehicle,inRemarks";
			}
			//主管审核 步骤编码SupervisorReview
			if (prcno == "SupervisorReview") 
			{
				UpdateFields = "inSupervisorApproval,inSupervisorOpinion";
			}
			//测试 步骤编码Test
			if (prcno == "Test") 
			{
				UpdateFields = "inNumber,inApplicant,inApplicationDepartment,inApplicationDate,inStartDate,inEndDate,inCauseAndPurpose,inCostEstimate,inVehicle,inRemarks,inSupervisorApproval,inSupervisorOpinion";
			}
			return UpdateFields;
		}


	}
}